/* File:        	AuthzServiceCaller.java
* Package:			com.tecnalia.authz.pep.ws.client
* Created:      	29-abr-2011
* Last Changed: 	0:11:43
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.pep.ws.client;

import java.rmi.RemoteException;
import java.util.Random;

import org.apache.axis2.AxisFault;
import com.tecnalia.authz.pep.ws.AuthzServiceStub;
import com.tecnalia.authz.pep.ws.ExceptionException;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.EvaluateFlexibleRequest;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.EvaluateFlexibleRequestResponse;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.EvaluateRequest;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.EvaluateRequestResponse;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.EvaluateStrictRequest;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.EvaluateStrictRequestResponse;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.GetAuthorizacion;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.GetAuthorizacionAndAccess;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.GetAuthorizacionAndAccessResponse;
import com.tecnalia.authz.pep.ws.AuthzServiceStub.GetAuthorizacionResponse;



// TODO: Auto-generated Javadoc
/**
 * The Class AuthzServiceCaller.
 */
public class AuthzServiceCaller {
	
	/** The stub. */
	private AuthzServiceStub stub = null;
	
	/** The authz. */
	private GetAuthorizacion authz = null;
	
	/** The authz access. */
	private GetAuthorizacionAndAccess authzAccess = null;
	
	/** The authz evaluate flexible request. */
	private EvaluateFlexibleRequest authzEvaluateFlexibleRequest = null;
	
	/** The authz strict request. */
	private EvaluateStrictRequest authzStrictRequest = null;
	
	/** The authz request. */
	private EvaluateRequest authzRequest = null;
	
    /** The Constant DECISION_PERMIT. */
    public static final int DECISION_PERMIT = 0;

    /** The Constant DECISION_DENY. */
    public static final int DECISION_DENY = 1;

    /** The Constant DECISION_INDETERMINATE. */
    public static final int DECISION_INDETERMINATE = 2;

    /** The Constant DECISION_NOT_APPLICABLE. */
    public static final int DECISION_NOT_APPLICABLE = 3;
	
    /** The Constant XACML_QUERY_NOT_STABLISHED. */
    public static final int XACML_QUERY_NOT_STABLISHED = 100;
	
	/** The authz client config. */
	public AuthzClientConfig authzClientConfig;
	
	
	/**
	 * Instantiates a new authz service caller.
	 *
	 * @param fileName the file name
	 * @throws AxisFault the axis fault
	 */
	public AuthzServiceCaller(String fileName) throws AxisFault {
		this.authzClientConfig = new AuthzClientConfig(fileName);	
		
		
		stub = new AuthzServiceStub(createEndpoint());
		authz = new AuthzServiceStub.GetAuthorizacion();
		authzAccess = new AuthzServiceStub.GetAuthorizacionAndAccess();
		authzEvaluateFlexibleRequest = new AuthzServiceStub.EvaluateFlexibleRequest();
		authzStrictRequest = new EvaluateStrictRequest();
		authzRequest = new EvaluateRequest();

	}
	
	/**
	 * Instantiates a new authz service caller.
	 *
	 * @throws AxisFault the axis fault
	 */
	public AuthzServiceCaller() throws AxisFault {
		this.authzClientConfig = new AuthzClientConfig();

		stub = new AuthzServiceStub(createEndpoint());
		authz = new AuthzServiceStub.GetAuthorizacion();
		authzAccess = new AuthzServiceStub.GetAuthorizacionAndAccess();
		authzEvaluateFlexibleRequest = new AuthzServiceStub.EvaluateFlexibleRequest();
		authzStrictRequest = new EvaluateStrictRequest();
		authzRequest = new EvaluateRequest();
	}

	/**
	 * Checks if is autorized.
	 *
	 * @return the int[]
	 * @throws RemoteException the remote exception
	 */
	public int[] isAutorized() throws RemoteException{
		int[] decision = {0,0,0,0};
		if( this.authzClientConfig.getSimulate() ){
			Random rand = new Random();
			decision[rand.nextInt(2)] = 1; 
		}else{
			GetAuthorizacionResponse resAuthz = stub.getAuthorizacion(authz);
			 if(resAuthz.get_return() == XACML_QUERY_NOT_STABLISHED){
				 //An Error Ocurred 
				 decision[DECISION_NOT_APPLICABLE] = 1;
				 
			 }else{
					decision[resAuthz.get_return()] = 1;				 
			 }
			 
		}
		
		return decision;		
	}
	
	/**
	 * Autorized access.
	 *
	 * @return the string
	 * @throws RemoteException the remote exception
	 */
	public String autorizedAccess() throws RemoteException{
		String response = null;
		if( this.authzClientConfig.getSimulate() ){
			response = "Random Resource"; 
		}else{
			GetAuthorizacionAndAccessResponse resAuthz = stub.getAuthorizacionAndAccess(authzAccess);			
			response = resAuthz.get_return();
		}
		
		return response;		
	}

	/**
	 * Evaluate flexible.
	 *
	 * @return the string[]
	 * @throws RemoteException the remote exception
	 * @throws ExceptionException the exception exception
	 */
	public String[] evaluateFlexible() throws RemoteException, ExceptionException{
		String[] response = null;
		if( this.authzClientConfig.getSimulate() ){
			String[] random  = new String[1];
			random[0] = "random_resource3";
			return random;
		}else{
			EvaluateFlexibleRequestResponse resAuthz = stub.evaluateFlexibleRequest(authzEvaluateFlexibleRequest);
			response = resAuthz.get_return();
		}
		
		return response;		
	}
	

	/**
	 * Evaluate strict.
	 *
	 * @return the int[]
	 * @throws RemoteException the remote exception
	 * @throws ExceptionException the exception exception
	 */
	public int[] evaluateStrict() throws RemoteException, ExceptionException{
		int[] decision = {0,0,0,0};
		if( this.authzClientConfig.getSimulate() ){
			decision[DECISION_DENY] = 1;
		}else{
			EvaluateStrictRequestResponse resAuthz = stub.evaluateStrictRequest(authzStrictRequest);
			int response = resAuthz.get_return();
			 if(response == XACML_QUERY_NOT_STABLISHED){
				 //An Error Ocurred 
				 decision[DECISION_NOT_APPLICABLE] = 1;
			 }else{
				 decision[response] = 1; 
			 }
		}	
		
		return decision;		
	}
	
	/**
	 * Evaluate request.
	 *
	 * @return the int[]
	 * @throws RemoteException the remote exception
	 * @throws ExceptionException the exception exception
	 */
	public int[] evaluateRequest() throws RemoteException, ExceptionException{
		int[] decision = {0,0,0,0};
		if( this.authzClientConfig.getSimulate() ){
			decision[DECISION_DENY] = 1;
		}else{
			EvaluateRequestResponse resAuthz = stub.evaluateRequest(authzRequest);
			int response = resAuthz.get_return();
			decision[response] = 1; 
		}
		
		return decision;		
	}

	/**
	 * Gets the subject.
	 *
	 * @return the subject
	 */
	public String getSubject() {
		return authz.getSubject();
	}

	/**
	 * Sets the subject.
	 *
	 * @param subject the new subject
	 */
	public void setSubject(String subject) {
		authz.setSubject(subject);
		authzAccess.setSubject(subject);
		authzEvaluateFlexibleRequest.setSubject(subject);
		authzStrictRequest.setSubject(subject);
		authzRequest.setSubject(subject);
	}

	/**
	 * Gets the role group.
	 *
	 * @return the role group
	 */
	public String getRoleGroup() {
		return authz.getRoleGroup();
	}

	/**
	 * Sets the role group.
	 *
	 * @param roleGroup the new role group
	 */
	public void setRoleGroup(String roleGroup) {
		authz.setRoleGroup(roleGroup);
		authzAccess.setRoleGroup(roleGroup);
		authzRequest.setRoleGroup(roleGroup);
	}

	/**
	 * Gets the res id.
	 *
	 * @return the res id
	 */
	public String getResID() {
		return authz.getResource();
	}

	/**
	 * Sets the res id.
	 *
	 * @param resID the new res id
	 */
	public void setResID(String resID) {
		authz.setResource(resID);
		authzAccess.setResource(resID);
		
		authzRequest.setResID(resID);
	}

	
	/**
	 * Sets the res id list.
	 *
	 * @param resIDList the new res id list
	 */
	public void setResIDList(String[] resIDList) {
		authzEvaluateFlexibleRequest.setServiceList(resIDList);
		authzStrictRequest.setServiceList(resIDList);
	}

	/**
	 * Gets the action.
	 *
	 * @return the action
	 */
	public String getAction() {
		return authz.getAction();
	}

	/**
	 * Sets the action.
	 *
	 * @param action the new action
	 */
	public void setAction(String action) {
		authz.setAction(action);
		authzAccess.setAction(action);
		authzEvaluateFlexibleRequest.setAction(action);
		authzStrictRequest.setAction(action);
		authzRequest.setAction(action);
	}
	

    /**
     * Creates the endpoint.
     *
     * @return the string
     */
    private String createEndpoint(){
    	String serviceEndpoint = this.authzClientConfig.getServiceEndpoint();
    	String[] splitted = serviceEndpoint.split("/");
    	
    	return  serviceEndpoint+"."+ splitted[splitted.length - 1] + "HttpSoap12Endpoint/";  
    	
    }
	
}
